package zelix.gui.cloudconfig;

import com.google.gson.*;
import zelix.gui.clickguis.caesium.components.ConfigFrame;
import zelix.hack.Hack;
import zelix.managers.FontManager;
import zelix.managers.HackManager;
import zelix.utils.Utils;
import zelix.utils.hooks.visual.ChatUtils;
import zelix.utils.hooks.visual.ColorUtils;
import zelix.utils.hooks.visual.HGLUtils;
import zelix.utils.hooks.visual.RenderUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;
import org.lwjgl.input.Mouse;
import zelix.value.*;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.Map;

import static zelix.command.Config.Send;

public class CloudConfig extends GuiScreen {

    public static Minecraft mc = Minecraft.getMinecraft();
    public static ScaledResolution sr = new ScaledResolution(mc);
    public static float startX = sr.getScaledWidth() / 2 - 450 / 2;
    public static float startY = sr.getScaledHeight() / 2 - 300 / 2;
    public float moveX = 0, moveY = 0;
    public static FontManager font=new FontManager() ;
    public static ConfigFrame[] NEW;
    public static ConfigFrame[] OWN = new ConfigFrame[1024];
    public static String Mode = "All";//All Own
    @Override
    public void initGui() {

    }

    @Override
    protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
        for (int i = 0; i < NEW.length; i++) {
            if (NEW[i] == null){
                continue;
            }

            if (i/6 +1 == nowpage){
                int x = i - (nowpage-1)*6;

                if (x > 2){
                    NEW[i].mouseClicked(mouseX,mouseY,mouseButton);
                }else{
                    NEW[i].mouseClicked(mouseX,mouseY,mouseButton);
                }
            }

            //////System.out.println("NEW[i] = " + NEW[i]);
        }

        if (isHovered(startX + 10, startY+280, startX + 100, startY + 300, mouseX, mouseY) && mouseButton == 0){
            nowpage = 1;
            if (Mode ==  "All"){
                Mode = "Own";
                OWN = new ConfigFrame[NEW.length];
                for (int i = 0; i < OWN.length; i++) {
                    if (OWN[i] == null){
                        continue;
                    }

                    int page = i/6;
                    ////System.out.println("page = " + page);
                    pages = page+1;
                }

            }else{
                Mode = "All";

                for (int i = 0; i < NEW.length; i++) {
                    if (NEW[i] == null){
                        continue;
                    }

                    int page = i/6;
                    ////System.out.println("page = " + page);
                    pages = page+1;
                }

            }
        }

        if (pages > 1){
            if (nowpage == 1){
                //+
                // RenderUtils.drawRect(startX + 210, startY+280, startX + 230, startY + 300 ,  ColorUtils.getColor(224,224,224));
                if (isHovered(startX + 210, startY+280, startX + 230, startY + 300, mouseX, mouseY) && mouseButton == 0 ) {
                    nowpage++;
                }
            }else if (nowpage == pages){
                //-
                // RenderUtils.drawRect(startX + 170, startY+280, startX + 190, startY + 300 ,  ColorUtils.getColor(224,224,224));
                if (isHovered(startX + 170, startY+280, startX + 190, startY + 300 , mouseX, mouseY) && mouseButton == 0) {
                    nowpage--;
                }
            }else {
                //-
                // RenderUtils.drawRect(startX + 175, startY+280, startX + 195, startY + 300 ,  ColorUtils.getColor(224,224,224));
                //+
                //  RenderUtils.drawRect(startX + 210, startY+280, startX + 230, startY + 300 ,  ColorUtils.getColor(224,224,224));
                if (isHovered(startX + 210, startY + 280, startX + 230, startY + 300, mouseX, mouseY) && mouseButton == 0) {
                    nowpage++;
                }
                if (isHovered(startX + 170, startY + 280, startX + 190, startY + 300, mouseX, mouseY) && mouseButton == 0) {
                    nowpage--;
                }
            }
        }
    }



    @Override
    protected void keyTyped(char typedChar, int keyCode) throws IOException {
        super.keyTyped(typedChar, keyCode);

    }

    public static String verify = "";
    public boolean LoadButton_isHovered = false;
    public String str = "";
    public static int pages;
    public int nowpage = 1;

    @Override
    public void drawScreen(int mouseX, int mouseY, float partialTicks) {
        //////System.out.println("666");
        //大界面
        RenderUtils.drawRect(startX, startY, startX + 400, startY + 310,  ColorUtils.getColor(96,96,96));
        font.getFont("SFR 14").drawString("Zelix Cloud Config Loader",startX+10,startY+10,ColorUtils.rainbow().getRGB());
        //输入框
        HGLUtils.drawGradientRect(startX+10, startY+30, startX + 300, startY + 40 + font.getFont("SFR 14").getHeight("HHHHHHH"),ColorUtils.getColor(224,224,224));
        font.getFont("SFR 14").drawString(str,startX+10, startY+35,ColorUtils.getColor(32,32,32));

        //按钮 Load
        RenderUtils.drawRect(startX + 320, startY+30, startX + 390, startY + 40 + font.getFont("SFR 14").getHeight("HHHHHHH"),  LoadButton_isHovered?ColorUtils.getColor(240,240,240):ColorUtils.getColor(224,224,224));

        font.getFont("SFR 14").drawString("Load",startX + 345, startY+35,ColorUtils.getColor(32,32,32));
        font.getFont("SFR 11").drawString("Click [LOAD] Button To Choose Your Private Config Verification File And Load It!",startX+10, startY+50,ColorUtils.getColor(32,32,32));

        if ((isHovered(startX, startY, startX + 450, startY + 50, mouseX, mouseY)
                || isHovered(startX, startY + 315, startX + 450, startY + 350, mouseX, mouseY)
                || isHovered(startX + 430, startY, startX + 450, startY + 350, mouseX, mouseY))
                && Mouse.isButtonDown(0)) {
            if (moveX == 0 && moveY == 0) {
                moveX = mouseX - startX;
                moveY = mouseY - startY;
            } else {
                startX = mouseX - moveX;
                startY = mouseY - moveY;
            }
            //this.previousmouse = true;
        } else if (moveX != 0 || moveY != 0) {
            moveX = 0;
            moveY = 0;
        }

        if (isHovered(startX + 320, startY+30, startX + 390, startY + 40 + font.getFont("SFR 14").getHeight("HHHHHHH"), mouseX, mouseY)){
            LoadButton_isHovered = true;
            if(Mouse.isButtonDown(0)){
                ////System.out.println("Loading...");
                JFrame frame = new JFrame();
                JFileChooser chooser=new JFileChooser(".");
                int i = chooser.showOpenDialog(frame);
                if (i == JFileChooser.APPROVE_OPTION){
                    str=Utils.readFile(chooser.getSelectedFile().getAbsolutePath());
                }

            }
        }else{
            LoadButton_isHovered = false;
        }



        int y = 0;
        for (int i = 0; i < NEW.length; i++) {

            if (NEW[i] == null){
                continue;
            }

            if (Mode == "Own"){
                if (NEW[i].isUserDev()){
                    OWN[y] = NEW[i];
                    y++;
                }
                continue;
            }

            if (i/6 +1 == nowpage){
                int x = i - (nowpage-1)*6;

                if (x > 2){
                    NEW[i].render((int)startX+10 + (x-3)*10 + (x-3)*120,(int)startY+65 + 110 ,0,mouseX,mouseY);
                }else{
                    NEW[i].render((int)startX+10 + x*10 + x*120,(int)startY+65 ,0,mouseX,mouseY);
                }
            }

            //////System.out.println("NEW[i] = " + NEW[i]);
        }

        if (Mode == "Own"){
            for (int i = 0; i < OWN.length; i++) {
                if (OWN[i] == null){
                    continue;
                }

                if (i/6 +1 == nowpage){
                    int x = i - (nowpage-1)*6;

                    if (x > 2){
                        OWN[i].render((int)startX+10 + (x-3)*10 + (x-3)*120,(int)startY+65 + 110 ,0,mouseX,mouseY);
                    }else{
                        OWN[i].render((int)startX+10 + x*10 + x*120,(int)startY+65 ,0,mouseX,mouseY);
                    }
                }
            }

        }

        font.getFont("SFR 14").drawString(String.valueOf(nowpage),startX + 200, startY+285,ColorUtils.rainbow().getRGB());

        if (pages > 1){
            if (nowpage == 1){
                //+
                RenderUtils.drawRect(startX + 210, startY+280, startX + 230, startY + 300 ,  ColorUtils.getColor(224,224,224));
                }else if (nowpage == pages){
                //-
                RenderUtils.drawRect(startX + 170, startY+280, startX + 190, startY + 300 ,  ColorUtils.getColor(224,224,224));
            }else{
                //-
                RenderUtils.drawRect(startX + 175, startY+280, startX + 195, startY + 300 ,  ColorUtils.getColor(224,224,224));
                //+
                RenderUtils.drawRect(startX + 210, startY+280, startX + 230, startY + 300 ,  ColorUtils.getColor(224,224,224));
            }
        }

        RenderUtils.drawRect(startX + 10, startY+280, startX + 100, startY + 300,
                isHovered(startX + 10, startY+280, startX + 100, startY + 300, mouseX, mouseY)
                        ? new Color(80, 80, 80, 121).getRGB()
                        : new Color(56, 56, 56, 121).getRGB());

        font.getFont("SFR 14").drawString(Mode,startX + 30, startY+285,ColorUtils.rainbow().getRGB());




    }

    public static boolean isHovered(float x, float y, float x2, float y2, int mouseX, int mouseY) {
        if (mouseX >= x && mouseX <= x2 && mouseY >= y && mouseY <= y2) {
            return true;
        }

        return false;
    }

    int ConfigNumbers;
    public void drawConfigList(){

    }
    public static void LoadConfig(String UUID){
        //115.126.43.76
        String Config = Send("115.126.43.76",9990,verify+"[TARGET]["+"LOAD"+"]"+"[UUID]["+UUID+"]");
        // ////System.out.println("Config = " + Config);
        if (Config != null){
            loadHacks(Config);

        }else{
            ChatUtils.error("null config");
        }

    }
    public static void UpdateConfig(String UUID,String Name){
        //isCloudUsing = true;
        //115.126.43.76

        String Config = Send("115.126.43.76",9990,verify+"[TARGET]["+"UPDATE"+"]"+"[NAME]["+Name+"]"+"[UUID]["+UUID+"]"+"[IN]["+GETIN()+"]");
        ChatUtils.message(Config);
    }
    public static void UploadConfig(String Name){
        //isCloudUsing = true;
        //115.126.43.76



        new Thread() {
            @Override
            public void run() {
                String s = verify+"[TARGET]["+"UPLOAD"+"]"+"[NAME]["+Name+"]"+"[IN]["+GETIN()+"]";
                ////System.out.println(s);
                String Config = Send("115.126.43.76",9990,s);
                ChatUtils.message(Config);
                super.run();
            }
        }.start();

    }


    private static JsonParser jsonParser = new JsonParser();

    public static void loadHacks(String IN) {
        BufferedReader bufferedReader = new BufferedReader(new StringReader(IN));
        JsonObject jsonObject = (JsonObject) jsonParser.parse(bufferedReader);
        try {
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
            Hack module = HackManager.getHack(entry.getKey());

            if (module == null)
                continue;

            JsonObject jsonObjectHack = (JsonObject) entry.getValue();

            module.setKey(jsonObjectHack.get("key").getAsInt());
            module.setToggled(jsonObjectHack.get("toggled").getAsBoolean());

            if (module.getValues().isEmpty())
                continue;

            for (Value value : module.getValues()) {

                if (value == null)
                    continue;

                if(jsonObjectHack.get(value.getName())==null) {
                    if (value instanceof BooleanValue) {
                        jsonObjectHack.addProperty(value.getName(), (Boolean) value.getValue());
                    }
                    if (value instanceof NumberValue) {
                        jsonObjectHack.addProperty(value.getName(), (Number) value.getValue());
                    }
                    if (value instanceof ModeValue) {
                        jsonObjectHack.addProperty(value.getName(), (Boolean) value.getValue());
                    }
                    //saveHacks();
                }

                if (value instanceof BooleanValue)
                    value.setValue(jsonObjectHack.get(value.getName()).getAsBoolean());
                if (value instanceof NumberValue)
                    value.setValue(jsonObjectHack.get(value.getName()).getAsDouble());
                if (value instanceof ModeValue) {
                    ModeValue modeValue = (ModeValue) value;
                    for (zelix.value.Mode mode : modeValue.getModes())
                        mode.setToggled(jsonObjectHack.get(mode.getName()).getAsBoolean());
                }
            }

        }
        ChatUtils.message("Loaded!");

    }

    public static String GETIN() {
        try {
            JsonObject json = new JsonObject();

            for (Hack module : HackManager.getHacks()) {
                JsonObject jsonHack = new JsonObject();
                jsonHack.addProperty("toggled", module.isToggled());
                jsonHack.addProperty("key", module.getKey());

                if (!module.getValues().isEmpty()) {
                    for (Value value : module.getValues()) {
                        if (value instanceof BooleanValue) {
                            jsonHack.addProperty(value.getName(), (Boolean) value.getValue());
                        }
                        if (value instanceof NumberValue) {
                            jsonHack.addProperty(value.getName(), (Number) value.getValue());
                        }
                        if (value instanceof ModeValue) {

                            ModeValue modeValue = (ModeValue) value;
                            for (Mode mode : modeValue.getModes()) {
                                jsonHack.addProperty(mode.getName(), mode.isToggled());
                            }
                        }
                    }
                }
                json.add(module.getName(), jsonHack);
            }

            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
